package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.*;
import com.sg.entity.SceneRole;
import com.sg.service.base.framework.MSceneRoleService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.SceneRoleMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MSceneRoleServiceImpl
        extends ServiceImpl<SceneRoleMapper, SceneRole>
        implements MSceneRoleService {

    @Autowired
    SceneRoleMapper sceneRoleMapper;

    /**
     * 1-2-08批量角色Code查询角色列表[1167]
     * gen by moon at 10/16/2022, 11:06:43 AM
     */
    @Trace(operationName = "1-2-08批量角色Code查询角色列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SceneRole> batchQueryByCodeRoleList(BatchQueryByCodeRoleListReq reqDto) {

        try {
            QueryWrapper<SceneRole> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getRoleCodeList() == null || reqDto.getRoleCodeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_code", reqDto.getRoleCodeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SceneRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<SceneRole>) super.list(queryWrapper)).stream().map(item -> {
                item.setRoleLogo(CommonFunctionHelper.getFilePath(item.getRoleLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08批量角色ID查询角色列表[1166]
     * gen by moon at 10/16/2022, 11:06:42 AM
     */
    @Trace(operationName = "1-2-08批量角色ID查询角色列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SceneRole> batchQueryByIdRoleList(BatchQueryByIdRoleListReq reqDto) {

        try {
            QueryWrapper<SceneRole> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getRoleIdList() == null || reqDto.getRoleIdList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_id", reqDto.getRoleIdList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SceneRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<SceneRole>) super.list(queryWrapper)).stream().map(item -> {
                item.setRoleLogo(CommonFunctionHelper.getFilePath(item.getRoleLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08批量查询角色列表[700]
     * gen by moon at 10/16/2022, 11:06:41 AM
     */
    @Trace(operationName = "1-2-08批量查询角色列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SceneRole> batchQueryRoleList(BatchQueryRoleListReq reqDto) {

        try {
            QueryWrapper<SceneRole> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getRoleList() == null || reqDto.getRoleList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_id", reqDto.getRoleList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SceneRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<SceneRole>) super.list(queryWrapper)).stream().map(item -> {
                item.setRoleLogo(CommonFunctionHelper.getFilePath(item.getRoleLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08查询角色详情[170]
     * gen by moon at 5/1/2023, 11:35:30 AM
     */
    @Trace(operationName = "1-2-08查询角色详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SceneRole queryRoleDetail(QueryRoleDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<SceneRole> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<SceneRole> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                SceneRole retData = retList.get(0);
                retData.setRoleLogo(CommonFunctionHelper.getFilePath(retData.getRoleLogo()));//otherModel: ALL_PATH
                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08查询角色列表[76]
     * gen by moon at 10/16/2022, 11:06:40 AM
     */
    @Trace(operationName = "1-2-08查询角色列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SceneRole> queryRoleList(QueryRoleListReq reqDto) {

        try {
            QueryWrapper<SceneRole> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SceneRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<SceneRole>) super.list(queryWrapper)).stream().map(item -> {
                item.setRoleLogo(CommonFunctionHelper.getFilePath(item.getRoleLogo()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08删除角色[75]
     * gen by moon at 10/16/2022, 11:06:39 AM
     */
    @Trace(operationName = "1-2-08删除角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRole(String roleId) {

        return super.removeById(roleId);
    }

    /**
     * 1-2-08修改角色[74]
     * gen by moon at 10/16/2022, 11:06:39 AM
     */
    @Trace(operationName = "1-2-08修改角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRole(SceneRole sceneRole) {
//     // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(sceneRole.getRoleLogo())){
//         sceneRole.setRoleLogo(CommonFunctionHelper.replaceFilePath(sceneRole.getRoleLogo()));
//     }
        sceneRole.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (sceneRole != null && sceneRole.getSubjectLifeCycle() != null && sceneRole.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            sceneRole.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (sceneRole != null && sceneRole.getSubjectLifeCycle() != null && sceneRole.getSubjectLifeCycle().equals("ARCHIVING")) {
            sceneRole.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        sceneRole.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (sceneRole != null && sceneRole.getSubjectLifeCycle() != null && sceneRole.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            sceneRole.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (sceneRole != null && sceneRole.getSubjectLifeCycle() != null && sceneRole.getSubjectLifeCycle().equals("ARCHIVING")) {
            sceneRole.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(sceneRole);
    }

    /**
     * 1-2-08新增角色[73]
     * gen by moon at 11/4/2022, 7:14:43 AM
     */
    @Trace(operationName = "1-2-08新增角色")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addRole(SceneRole sceneRole) {
        // TODO: 2024/1/20
//     if(!StringUtils.isEmpty(sceneRole.getRoleLogo())){
//         sceneRole.setRoleLogo(CommonFunctionHelper.replaceFilePath(sceneRole.getRoleLogo()));
//     }
        sceneRole.setRoleId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        sceneRole.setIsFreeze("FALSE");
        sceneRole.setIsValid("FALSE");
        sceneRole.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        sceneRole.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (sceneRole != null && sceneRole.getSubjectLifeCycle() != null && sceneRole.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            sceneRole.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        sceneRole.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        sceneRole.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (sceneRole != null && sceneRole.getSubjectLifeCycle() != null && sceneRole.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            sceneRole.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        sceneRole.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        sceneRole.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        sceneRole.setIsArchive("FALSE");
        sceneRole.setIsRecommend("FALSE");
        if (super.save(sceneRole)) {
            return sceneRole.getRoleId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-08查询角色列表ByCodes[3224]
     * gen by moon at 10/16/2022, 11:06:43 AM
     */
    @Trace(operationName = "1-2-08查询角色列表ByCodes")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SceneRole> batchQueryRoleListByCodes(BatchQueryRoleListByCodesReq reqDto) {

        try {
            QueryWrapper<SceneRole> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getRoleCodeList() == null || reqDto.getRoleCodeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_code", reqDto.getRoleCodeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(SceneRole.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(SceneRole.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<SceneRole>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
